defmodule Formatter do
    @moduledoc false

    import Jason

    def new_short_uid do
      UUID.uuid4() |> ShortUUID.encode!()
    end

    # def build_vcode() do
    #   (:random.uniform() * 1000_000) |> round |> to_string
    # end

    def schema_to_map(s) do
      s |> Map.from_struct() |> Map.delete(:__meta__)
    end

    def clear_info(p, clear_list) when is_map(p) and is_list(clear_list) do
      Map.drop(p, clear_list)
    end

    def clear_info(p, _) do
      p
    end

    def encode_json(obj) do
      obj |> Jason.encode([{:escape, :unicode_safe}])
    end

    def encode_json!(obj) do
      obj |> Jason.encode!([{:escape, :unicode_safe}])
    end

    def decode_json(str) do
      str |> Jason.decode([{:escape, :unicode_safe}])
    end

    def decode_json!(str) do
      str |> Jason.decode!([{:escape, :unicode_safe}])
    end

  end

  # For jason to encode some elixir types
  defimpl Jason.Encoder, for: [MapSet, Range, Stream] do
    def encode(struct, opts) do
      Jason.Encode.list(Enum.to_list(struct), opts)
    end
  end

  # Enumerable not implemented for Tuple, use Tuple.to_list
  defimpl Jason.Encoder, for: [Tuple] do
    def encode(struct, opts) do
      Jason.Encode.list(Tuple.to_list(struct), opts)
    end
  end
